﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pokerServer.GameManagment.GameTools {

	public class CardsDeck {

		Card[][] gCards;
		Random rand;
		int gCardsAmount;

		public CardsDeck() {
			this.resetCards();
		}
		public void resetCards() {
			gCards = new Card[4][];
			rand = new Random();
			gCardsAmount = 52;
			for (int i = 0; i < gCards.Length; i++) {
				gCards[i] = new Card[13];
			}
			//Console.WriteLine("gCards[i%4][i%13] = new Card((i % 4) + 1, (i % 13) + 2)");
			for (int i = 0; i < 52; i++) {
				gCards[i % 4][i % 13] = new Card((i % 4) + 1, (i % 13) + 2);
				//Console.WriteLine("gCards[{0}][{1}] = new Card({2}, {3})\tCard: {4}", i % 4, i % 13, (i % 4) + 1, (i % 13) + 2, gCards[i % 4][i % 13]);
			}
		}
		private Card RescaleCards(int i, int j) {
			Card[] tCards = new Card[gCards[i].Length - 1];
			Card tCard = gCards[i][j];
			Boolean flag = false;
			for (int z = 0; z < tCards.Length; z++) {
				if (z == j) flag = true;
				if (flag) tCards[z] = gCards[i][z + 1];
				else tCards[z] = gCards[i][z];
			}
			gCards[i] = tCards;
			gCardsAmount--;
			return tCard;
		}
		public Card DrawCard {
			get {
				int cn = rand.Next(0, gCardsAmount);
				int sum = 0, i = 0, j = cn;
				for (i = 0; i < 4; i++) {
					if (gCards[i].Length == 0) { continue; }
					if (cn < sum + gCards[i].Length) {
						j = cn - sum;
						break;
					}
					else sum += gCards[i].Length;
				}
				Card tcard = gCards[i][j];
				this.RescaleCards(i, j);
				return tcard;
			}
		}
		public void printDeck() {
			Console.WriteLine("Cards Deck:");
			//Tools.MyFunc.printCardDeck(gCards);
		}
	}
}
